package com.simin.mall.ui

import android.annotation.SuppressLint
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import androidx.core.content.ContextCompat
import com.simin.mall.R
import com.simin.mall.base.BaseFragment
import com.simin.mall.base.BaseKtActivity
import com.simin.mall.databinding.ActMainLayoutBinding
import com.simin.mall.ui.fragment.CateFragment
import com.simin.mall.ui.fragment.HomeFragment
import com.simin.mall.ui.fragment.MineFragment
import com.simin.mall.ui.fragment.ShopCartFragment
import com.simin.mall.utils.StringUtils

/**
 * 首页
 */
class MainActivity : BaseKtActivity<ActMainLayoutBinding>(), View.OnClickListener {
    var mCurrentFragment: BaseFragment? = null
    var mHomeFragment: BaseFragment? = null
    var mCateFragment: BaseFragment? = null
    var mShopcartFragment: BaseFragment? = null
    var mMineFragment: BaseFragment? = null
    override fun inflateBinding(inflater: LayoutInflater): ActMainLayoutBinding {
        return ActMainLayoutBinding.inflate(inflater)
    }

    override fun initData() {
        binding.homeTv.setOnClickListener(this)
        binding.cateTv.setOnClickListener(this)
        binding.shopCartTv.setOnClickListener(this)
        binding.mineTv.setOnClickListener(this)

        switchToHome()
        setTabSelectStatus(0)
    }

    /**
     * 点击事件
     */
    override fun onClick(v: View?) {
        if (v == binding.homeTv) {
            switchToHome()
            setTabSelectStatus(0)
        } else if (v == binding.cateTv) {
            switchToCate()
            setTabSelectStatus(1)
        } else if (v == binding.shopCartTv) {
            switchToShopCart()
            setTabSelectStatus(2)
        } else if (v == binding.mineTv) {
            switchToMine()
            setTabSelectStatus(3)
        }
    }

    /**
     * 切换首页
     */
    fun switchToHome() {
        if (mHomeFragment == null) {
            val homeFragment = HomeFragment()
            mHomeFragment = homeFragment
        }
        replace(mHomeFragment!!, "home")
    }

    /**
     * 切换分类
     */
    fun switchToCate() {
        if (mCateFragment == null) {
            val cateFragment = CateFragment()
            mCateFragment = cateFragment
        }
        replace(mCateFragment!!, "class")
    }

    /**
     * 切换购物车
     */
    fun switchToShopCart() {
        if (mShopcartFragment == null) {
            val shopCartFragment = ShopCartFragment()
            mShopcartFragment = shopCartFragment
        }
        replace(mShopcartFragment!!, "shopCart")
    }

    /**
     * 切换我的
     */
    fun switchToMine() {
        if (mMineFragment == null) {
            val mineFragment = MineFragment()
            mMineFragment = mineFragment
        }
        replace(mMineFragment!!, "mine")
    }


    /**
     * 显示隐藏fragment
     */
    @SuppressLint("CommitTransaction")
    fun replace(fragment: BaseFragment, tag: String) {
        if (mCurrentFragment != fragment) {
            val transaction = supportFragmentManager.beginTransaction()
            if (mCurrentFragment != null) {
                transaction.hide(mCurrentFragment!!)
            }
            mCurrentFragment = fragment
            if (!fragment.isAdded) {
                transaction.add(R.id.main_Layout, fragment, tag).show(fragment).commit()
            } else {
                transaction.show(fragment).commit()
            }
        }
    }


    /**
     * 设置tab切换状态
     */
    fun setTabSelectStatus(index: Int) {
        //首页文字和icon颜色
        binding.homeTv.setTextColor(
            ContextCompat.getColor(
                this,
                if (index == 0) R.color.theme_pink_red else R.color.theme_gray
            )
        )
        val homeIcon = StringUtils.getTextImage(this, R.mipmap.ic_tab_home)
        val homeColor = ContextCompat.getColor(
            this,
            if (index == 0) R.color.theme_pink_red else R.color.theme_gray
        )
        homeIcon.setTint(homeColor)
        binding.homeTv.setCompoundDrawables(null, homeIcon, null, null)
        //分类文字和icon颜色
        binding.cateTv.setTextColor(
            ContextCompat.getColor(
                this,
                if (index == 1) R.color.theme_pink_red else R.color.theme_gray
            )
        )
        val cateIcon = StringUtils.getTextImage(this, R.mipmap.ic_tab_cate)
        val cateColor = ContextCompat.getColor(
            this,
            if (index == 1) R.color.theme_pink_red else R.color.theme_gray
        )
        cateIcon.setTint(cateColor)
        binding.cateTv.setCompoundDrawables(null, cateIcon, null, null)
        //购物车文字和icon颜色
        binding.shopCartTv.setTextColor(
            ContextCompat.getColor(
                this,
                if (index == 2) R.color.theme_pink_red else R.color.theme_gray
            )
        )
        val cartIcon = StringUtils.getTextImage(this, R.mipmap.ic_tab_cart)
        val cartColor = ContextCompat.getColor(
            this,
            if (index == 2) R.color.theme_pink_red else R.color.theme_gray
        )
        cartIcon.setTint(cartColor)
        binding.shopCartTv.setCompoundDrawables(null, cartIcon, null, null)
        //我的文字和icon颜色
        binding.mineTv.setTextColor(
            ContextCompat.getColor(
                this,
                if (index == 3) R.color.theme_pink_red else R.color.theme_gray
            )
        )
        val mineIcon = StringUtils.getTextImage(this, R.mipmap.ic_tab_mine)
        val mineColor = ContextCompat.getColor(
            this,
            if (index == 3) R.color.theme_pink_red else R.color.theme_gray
        )
        mineIcon.setTint(mineColor)
        binding.mineTv.setCompoundDrawables(null, mineIcon, null, null)
    }


    /**
     * 返回键处理事件
     */
    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //仿返回键退出界面但不消毁程序仍活在后台
            moveTaskToBack(true)
        }
        return super.onKeyDown(keyCode, event)
    }
}
